import org.w3c.dom.Node;

import server.ClientState;
import server.IProtocolHandler;
import xml.Message;
import dd.server.AdminRetrieveAllEventsResponseController;
import dd.server.GenerateReportResponseController;
import dd.server.addChoiceResponseController;
import dd.server.addEdgeResponseController;
import dd.server.createResponseController;
import dd.server.signInResponseController;

/**
 * Sample implementation of a protocol handler to respond to messages received from clients. You should follow this
 * template when designing YOUR protocol handler.
 * <p>
 * To avoid issues with multiple clients submitting requests concurrently, simply make the
 * {@link #process(ClientState, Message)} method synchronized. This will ensure that no more than one server thread
 * executes this method at a time.
 * <p>
 * Also extended to support detection of client disconnects so these can release the lock if indeed the client was the
 * one locking the model.
 */
public class SampleProtocolHandler implements IProtocolHandler {

	public synchronized Message process(ClientState st, Message request) {
		Node child = request.contents.getFirstChild();
		String type = child.getLocalName();

		System.out.println("==========+===========");
		System.out.println(SampleProtocolHandler.class.getName() + ": Server received request type is : " + type);
		System.out.println("==========+===========");

		// send back our model
		if (type.equals("signInRequest")) {
			return new signInResponseController().process(st, request);
		} else if (type.equals("createRequest")) {
			return new createResponseController().process(st, request);
		} else if (type.equals("addChoiceRequest")) {
			return new addChoiceResponseController().process(st, request);
		} else if (type.equals("addEdgeRequest")) {
			return new addEdgeResponseController().process(st, request);
		} else if (type.equals("reportRequest")) {
			return new GenerateReportResponseController().process(st, request);
		} else if (type.equals("eventsRequest")) {
			return new AdminRetrieveAllEventsResponseController().process(st, request);
		}

		// unknown? no idea what to do
		System.err.println("Unable to handle message:" + request);
		return null;
	}
}
